
{***********************************************************}
{                                                           }
{      Informix esql "sqltypes.h"                           }
{                                                           }
{      Translated to Object Pascal by                       }
{      Copyright (c) 2001-2008 Luxena Software Company      }
{      http://www.luxena.com                                }
{      e-mail: support@luxena.com                           }
{                                                           }
{***********************************************************}

unit IfxSqlTypes;

{$I lx.inc}

interface

uses
  IfxTypes, IfxCollect, IfxStubUnit;

const

  //***********************
  // ++++ CAUTION ++++
  // Any new type to be added to the following lists should not
  // have the following bit pattern (binary short):
  //
  //	xxxxx111xxxxxxxx
  //
  // where x can be either 0 or 1.
  //
  // This is due to the use of the bits as SQLNONULL, SQLHOST and SQLNETFLT
  // (see below).
  //
  // FAILURE TO DO SO WOULD RESULT IN POSSIBLE ERRORS DURING CONVERSIONS.
  //
  //***********************

   // C language types

  CCHARTYPE         = 100;
  CSHORTTYPE        = 101;
  CINTTYPE          = 102;
  CLONGTYPE         = 103;
  CFLOATTYPE        = 104;
  CDOUBLETYPE       = 105;
  CDECIMALTYPE      = 107;
  CFIXCHARTYPE      = 108;
  CSTRINGTYPE       = 109;
  CDATETYPE         = 110;
  CMONEYTYPE        = 111;
  CDTIMETYPE        = 112;
  CLOCATORTYPE      = 113;
  CVCHARTYPE        = 114;
  CINVTYPE          = 115;
  CFILETYPE         = 116;
  CINT8TYPE         = 117;
  CCOLLTYPE         = 118;
  CLVCHARTYPE       = 119;
  CFIXBINTYPE       = 120;
  CVARBINTYPE       = 121;
  CBOOLTYPE         = 122;
  CROWTYPE          = 123;
  CLVCHARPTRTYPE    = 124;
  CTYPEMAX          = 25;


  COLLMASK          = $007F; // mask out CTYPEDCOLL or CCLIENTCOLL bit set for CCOLLTYPE or CROWTYPE


  // The following are for client side only. They are included here
  // because of the above related definitions.

  CTYPEDCOLL        = $0080; // typed collection bit
  CTYPEDCOLLUNMASK  = $FF7F; // unmask typed collection bit


  // Define all possible database types
  // include C-ISAM types here as well as in isam.h

  SQLCHAR           = 0;
  SQLSMINT          = 1;
  SQLINT            = 2;
  SQLFLOAT          = 3;
  SQLSMFLOAT        = 4;
  SQLDECIMAL        = 5;
  SQLSERIAL         = 6;
  SQLDATE           = 7;
  SQLMONEY          = 8;
  SQLNULL           = 9;
  SQLDTIME          = 10;
  SQLBYTES          = 11;
  SQLTEXT           = 12;
  SQLVCHAR          = 13;
  SQLINTERVAL       = 14;
  SQLNCHAR          = 15;
  SQLNVCHAR         = 16;
  SQLINT8           = 17;
  SQLSERIAL8        = 18;
  SQLSET            = 19;
  SQLMULTISET       = 20;
  SQLLIST           = 21;
  SQLROW            = 22;
  SQLCOLLECTION     = 23;
  SQLROWREF         = 24;


  // Note: SQLXXX values from 25 through 39 are reserved to avoid collision
  //       with reserved PTXXX values in that same range. See p_types_t.h
  //
  // REFSER8: create tab with ref: referenced serial 8 rsam counter
  //	this is essentially a SERIAL8, but is an additional rsam counter
  //	this type only lives in the system catalogs and when read from
  //	disk is converted to SQLSERIAL8 with CD_REFSER8 set in ddcol_t
  //    ddc_flags we must distinguish from SERIAL8 to allow both
  //    counters in one tab

  SQLUDTVAR         = 40;
  SQLUDTFIXED       = 41;
  SQLREFSER8        = 42;

  // These types are used by FE, they are not real major types in BE
  SQLLVARCHAR       = 43;
  SQLSENDRECV       = 44;
  SQLBOOL           = 45;
  SQLIMPEXP         = 46;
  SQLIMPEXPBIN      = 47;

  // This type is used by the UDR code to track default parameters,
  // it is not a real major type in BE
  SQLUDRDEFAULT     = 48;

  SQLMAXTYPES       = 49;

  SQLLABEL          = SQLINT;

  SQLTYPE           = $FF; // type mask

  SQLNONULL         = $0100; // disallow nulls

  // a bit to show that the value is from a host variable
  SQLHOST           = $0200; // Value is from host var.
  SQLNETFLT         = $0400; // float-to-decimal for networked backend
  SQLDISTINCT       = $0800; // distinct bit
  SQLNAMED          = $1000; // Named row type vs row type
  SQLDLVARCHAR      = $2000; // Distinct of lvarchar
  SQLDBOOLEAN       = $4000; // Distinct of boolean
  SQLCLIENTCOLL     = $8000; // Collection is processed on client

  CCLIENTCOLL       = SQLCLIENTCOLL; // client collection bit

  // we are overloading SQLDBOOLEAN for use with row types
  SQLVARROWTYPE     = $4000; // varlen row type


  // We overload SQLNAMED for use with constructor type, this flag
  // distinguish constructor types from other UDT types.
  //
  // Please do not test this bit directly, use macro ISCSTTYPE() instead.

  SQLCSTTYPE        = $1000; // constructor type flag

  TYPEIDMASK        = SQLTYPE or SQLDISTINCT or SQLNAMED or SQLDLVARCHAR or SQLDBOOLEAN;

  SIZCHAR           = 1;
  SIZSMINT          = 2;
  SIZINT            = 4;
  SIZFLOAT          = SizeOf(Double);
  SIZSMFLOAT        = SizeOf(Single);
  SIZDECIMAL        = 17; // decimal(32)
  SIZSERIAL         = 4;
  SIZDATE           = 4;
  SIZMONEY          = 17; // decimal(32)
  SIZDTIME          = 7;  // decimal(12,0)
  SIZVCHAR          = 1;
  SIZINT8           = SizeOf(SmallInt) + SizeOf(muint) * 2;
  SIZSERIAL8        = SIZINT8;
  SIZCOLL           = SizeOf(TIfx_collection);
  SIZSET            = SIZCOLL;
  SIZMULTISET       = SIZCOLL;
  SIZLIST           = SIZCOLL;
  SIZROWREF         = SizeOf(TIfx_ref);


  DEFDECIMAL        = 9; // default decimal(16) size
  DEFMONEY          = 9; // default decimal(16) size

  SYSPUBLIC         = 'public';



  // if an SQL type is specified, convert to default C type
  // map C int to either short or long


  TYPEMAX           = SQLMAXTYPES;


  // Extended ID definitions for predefined UDT's

  // These can not be changed because sqli is using
  // them.  If we change them, the client has to recompile.
  // NOTE: This order must match the definitions in boot90.sql

  XID_LVARCHAR      = 1;
  XID_SENDRECV      = 2;
  XID_IMPEXP        = 3;
  XID_IMPEXPBIN     = 4;
  XID_BOOLEAN       = 5;
  XID_POINTER       = 6;
  XID_INDEXKEYARRAY = 7;
  XID_RTNPARAMTYPES = 8;
  XID_SELFUNCARGS   = 9;
  XID_BLOB          = 10;
  XID_CLOB          = 11;
  XID_LOLIST        = 12;
  XID_STAT          = 15;
  XID_CLIENTBINVAL  = 16;


  // Max size definitions for the predefined UDT's.
  // Only a few are currently defined.

  SIZINDEXKEYARRAY  = 1024;
  SIZLVARCHAR       = 2048;
  SIZRTNPARAMTYPES  = 4096;
  SIZSTAT           = 272;


  // Alignment required by predefined UDT's.
  // Only a few are currently defined.  At a minimum,
  // all with alignment not 1 should be defined here
  // and used throughout the code.

  ALNINDEXKEYARRAY  = 4;
  ALNSTAT           = 8;


  USER_XTDTYPEID_START = 2048;


{--- Define section -----------------------------------------------------------}

  //* #define USERCOLL(x)	((x))
function USERCOLL(x: int2): int2;


//* #define ISCOLLECTIONVAR(n)  (((n) & COLLMASK) == CCOLLTYPE)
function ISCOLLECTIONVAR(n: int2): Boolean;

//* #define ISROWVAR(n)         (((n) & COLLMASK) == CROWTYPE)
function ISROWVAR(n: int2): Boolean;

//* #define ISCOLL_OR_ROWVAR(n)   ((ISCOLLECTIONVAR(n)) || (ISROWVAR(n)))
function ISCOLL_OR_ROWVAR(n: int2): Boolean;

//* #define ISCLIENTCOLLECTION(n) (ISCOLLECTIONVAR(n) && ((n) & CCLIENTCOLL))
function ISCLIENTCOLLECTION(n: int2): Boolean;

//* #define ISCLIENTCOMPLEX(n)    ((ISCLIENTCOLLECTION(n)) || (ISROWVAR(n)))
function ISCLIENTCOMPLEX(n: int2): Boolean;




//* #define ISTYPEDCOLLECTION(n)  (ISCOLLECTIONVAR(n) && ((n) & CTYPEDCOLL))
function ISTYPEDCOLLECTION(n: int2): Boolean;

//* #define ISTYPEDROW(n)         (ISROWVAR(n) && ((n) & CTYPEDCOLL))
function ISTYPEDROW(n: int2): Boolean;

//* #define ISTYPEDCOLL_OR_ROW(n)  ( (ISTYPEDCOLLECTION(n)) || (ISTYPEDROW(n)) )
function ISTYPEDCOLL_OR_ROW(n: int2): Boolean;


//* #define MASKNONULL(t)	((t) & (SQLTYPE))
function MASKNONULL(t: int2): int2;

//* #define ISSQLTYPE(t)	(MASKNONULL(t) >= SQLCHAR && MASKNONULL(t) < SQLMAXTYPES)
function ISSQLTYPE(t: int2): Boolean;




// SQL types macros

//* #define ISDECTYPE(t) 	(MASKNONULL(t) == SQLDECIMAL || MASKNONULL(t) == SQLMONEY || MASKNONULL(t) == SQLDTIME || MASKNONULL(t) == SQLINTERVAL)
function ISDECTYPE(t: int2): Boolean;

//* #define ISNUMERICTYPE(t)	(MASKNONULL(t) == SQLSMINT || \
//*        MASKNONULL(t) == SQLINT || \
//*        MASKNONULL(t) == SQLINT8 || \
//*        MASKNONULL(t) == SQLFLOAT || \
//*        MASKNONULL(t) == SQLSMFLOAT || \
//*        MASKNONULL(t) == SQLMONEY || \
//*        MASKNONULL(t) == SQLSERIAL || \
//*        MASKNONULL(t) == SQLSERIAL8 || \
//*          MASKNONULL(t) == SQLDECIMAL)
function ISNUMERICTYPE(t: int2): Boolean;

//* #define ISBLOBTYPE(type)	(ISBYTESTYPE (type) || ISTEXTTYPE(type))
function ISBLOBTYPE(sqltype: int2): Boolean;

//* #define ISBYTESTYPE(type)	(MASKNONULL(type) == SQLBYTES)
function ISBYTESTYPE(sqltype: int2): Boolean;

//* #define ISTEXTTYPE(type)	(MASKNONULL(type) == SQLTEXT)
function ISTEXTTYPE(sqltype: int2): Boolean;

//* #define ISSQLHOST(t)      (((t) & SQLHOST) == SQLHOST)
function ISSQLHOST(t: int2): Boolean;

//* #define ISVCTYPE(t)	  (MASKNONULL(t) == SQLVCHAR || MASKNONULL(t) == SQLNVCHAR)
function ISVCTYPE(t: int2): Boolean;

//* #define ISCHARTYPE(t) 	(MASKNONULL(t) == SQLCHAR || MASKNONULL(t) == SQLNCHAR)
function ISCHARTYPE(t: int2): Boolean;

//* #define ISNSTRINGTYPE(t)	(MASKNONULL(t) == SQLNCHAR || MASKNONULL(t) == SQLNVCHAR)
function ISNSTRINGTYPE(t: int2): Boolean;

//* #define ISSTRINGTYPE(t)		(ISVCTYPE(t) || ISCHARTYPE(t))
function ISSTRINGTYPE(t: int2): Boolean;

//* #define	ISUDTVARTYPE(t)		(MASKNONULL(t) == SQLUDTVAR)
function ISUDTVARTYPE(t: int2): Boolean;

//* #define	ISUDTFIXEDTYPE(t)	(MASKNONULL(t) == SQLUDTFIXED)
function ISUDTFIXEDTYPE(t: int2): Boolean;

//* #define	ISUDTTYPE(t)		(ISUDTVARTYPE(t) || ISUDTFIXEDTYPE(t))
function ISUDTTYPE(t: int2): Boolean;

//* #define	ISCOMPLEXTYPE(t)	(ISROWTYPE(t) || ISCOLLTYPE(t))
function ISCOMPLEXTYPE(t: int2): Boolean;

//* #define	ISROWTYPE(t)		(MASKNONULL(t) == SQLROW)
function ISROWTYPE(t: int2): Boolean;

//* #define	ISLISTTYPE(t)		(MASKNONULL(t) == SQLLIST)
function ISLISTTYPE(t: int2): Boolean;

//* #define	ISMULTISETTYPE(t)	(MASKNONULL(t) == SQLMULTISET)
function ISMULTISETTYPE(t: int2): Boolean;

//* #define	ISREFTYPE(t)		(MASKNONULL(t) == SQLROWREF)
function ISREFTYPE(t: int2): Boolean;

//* #define	ISSETTYPE(t)		(MASKNONULL(t) == SQLSET)
function ISSETTYPE(t: int2): Boolean;

//* #define	ISCOLLECTTYPE(t)	(MASKNONULL(t) == SQLCOLLECTION)
function ISCOLLECTTYPE(t: int2): Boolean;

//* #define ISCOLLTYPE(t)		(ISSETTYPE(t) || ISMULTISETTYPE(t) || ISLISTTYPE(t) || ISCOLLECTTYPE(t))
function ISCOLLTYPE(t: int2): Boolean;

//* #define ISSERIALTYPE(t)   	(((t) & SQLTYPE) == SQLSERIAL || ((t) & SQLTYPE) == SQLSERIAL8 || \
//*       ((t) & SQLTYPE) == SQLREFSER8)
function ISSERIALTYPE(t: int2): Boolean;

//* #define ISDISTINCTTYPE(t)	((t) & SQLDISTINCT)
function ISDISTINCTTYPE(t: int2): Boolean;

//* #define ISCSTTYPE(t)		(ISUDTTYPE(t) && ((t) & SQLCSTTYPE))
function ISCSTTYPE(t: int2): Boolean;


// these macros are used to distinguish NLS char types and non-nls (ASCII)
// char types

//* #define ISNONNLSCHAR(t)		(MASKNONULL(t) == SQLCHAR || \
//*        MASKNONULL(t) == SQLVCHAR)
function ISNONNLSCHAR(t: int2): Boolean;



//* #define ISOPTICALCOL(type)	(type == 'O')
function ISOPTICALCOL(sqltype: Char): Boolean;



// These macros should be used to test lvarchar and distinct of lvarchar

//* #define ISLVARCHARXTYPE(t, xid)  (ISUDTTYPE((t)) && (xid) == XID_LVARCHAR)
function ISLVARCHARXTYPE(t: int2; xid: int4): Boolean;

//* #define ISDISTINCTLVARCHAR(t)	((t) & SQLDLVARCHAR)
function ISDISTINCTLVARCHAR(t: int2): Boolean;

//* #define LIKELVARCHARXTYPE(t,xid) ((ISLVARCHARXTYPE(t,xid))||\
//*          ISDISTINCTLVARCHAR(t))
function LIKELVARCHARXTYPE(t: int2; xid: int4): Boolean;

//* #define ISSMARTBLOB(type, xid)  (ISUDTTYPE((type)) && \
//*           ((xid == XID_CLOB) || (xid == XID_BLOB)))
function ISSMARTBLOB(sqltype: int2; xid: int4): Boolean;


// These macros should be used to test boolean and distinct of boolean

//* #define ISBOOLEANXTYPE(t, xid)  (ISUDTTYPE((t)) && (xid) == XID_BOOLEAN)
function ISBOOLEANXTYPE(t: int2; xid: int4): Boolean;

//* #define ISDISTINCTBOOLEAN(t)	(((t) & SQLDBOOLEAN) && \
//*           (ISUDTTYPE(t)))
function ISDISTINCTBOOLEAN(t: int2): Boolean;

//* #define LIKEBOOLEANXTYPE(t,xid) ((ISBOOLEANXTYPE(t,xid))||\
//*         ISDISTINCTBOOLEAN(t))
function LIKEBOOLEANXTYPE(t: int2; xid: int4): Boolean;

//* #define ISFIXLENGTHTYPE(t)	(!ISBYTESTYPE(t) && !ISTEXTTYPE(t) \
//*        && !ISCOMPLEXTYPE(t) \
//*        && !ISUDTVARTYPE(t) \
//*        && !ISVCTYPE(t))
function ISFIXLENGTHTYPE(t: int2): Boolean;




type
  PInt2Array = ^TInt2Array;
  TInt2Array = array[0..0] of Int2;

var
  sqlctype          : PInt2Array;


(**

  extern int2 sqlctype[];

  #define toctype(ctype, type) \
      { \
      if (type == CINTTYPE) \
      { \
        if (sizeof(mint) == sizeof(mlong)) \
            { ctype = type = CLONGTYPE; } \
        else if (sizeof(mint) == sizeof(int2)) \
            { ctype = type = CSHORTTYPE; } \
        else \
            { ctype = CLONGTYPE; type = CINTTYPE; } \
      } \
      else if (type >= 0 && type < TYPEMAX) \
          ctype = sqlctype[type]; \
      else \
          ctype = type; \
      }
**)
procedure toctype(var ctype, sqltype: int2);



implementation

function USERCOLL(x: int2): int2;
begin
  // #define USERCOLL(x)	((x))
  Result := x;
end;

function ISCOLLECTIONVAR(n: int2): Boolean;
begin
  // #define ISCOLLECTIONVAR(n)  (((n) & COLLMASK) == CCOLLTYPE)
  Result := (n and COLLMASK) = CCOLLTYPE;
end;

function ISROWVAR(n: int2): Boolean;
begin
  // #define ISROWVAR(n)         (((n) & COLLMASK) == CROWTYPE)
  Result := (n and COLLMASK) = CROWTYPE;
end;

function ISCOLL_OR_ROWVAR(n: int2): Boolean;
begin
  // #define ISCOLL_OR_ROWVAR(n)   ((ISCOLLECTIONVAR(n)) || (ISROWVAR(n)))
  Result := (ISCOLLECTIONVAR(n) or ISROWVAR(n));
end;

function ISCLIENTCOLLECTION(n: int2): Boolean;
begin
  // #define ISCLIENTCOLLECTION(n) (ISCOLLECTIONVAR(n) && ((n) & CCLIENTCOLL))
  Result := ISCOLLECTIONVAR(n) and Boolean(n and CCLIENTCOLL);
end;

function ISCLIENTCOMPLEX(n: int2): Boolean;
begin
  // #define ISCLIENTCOMPLEX(n)    ((ISCLIENTCOLLECTION(n)) || (ISROWVAR(n)))
  Result := (ISCLIENTCOLLECTION(n) or ISROWVAR(n));
end;

function ISTYPEDCOLLECTION(n: int2): Boolean;
begin
  // #define ISTYPEDCOLLECTION(n)  (ISCOLLECTIONVAR(n) && ((n) & ctypeDCOLL))
  Result := ISCOLLECTIONVAR(n) and Boolean(n and ctypeDCOLL);
end;

function ISTYPEDROW(n: int2): Boolean;
begin
  // #define ISTYPEDROW(n)         (ISROWVAR(n) && ((n) & ctypeDCOLL))
  Result := ISROWVAR(n) and Boolean(n and ctypeDCOLL);
end;

function ISTYPEDCOLL_OR_ROW(n: int2): Boolean;
begin
  // #define ISTYPEDCOLL_OR_ROW(n)  ( (ISTYPEDCOLLECTION(n)) || (ISTYPEDROW(n)) )
  Result := (ISTYPEDCOLLECTION(n) or ISTYPEDROW(n));
end;

function MASKNONULL(t: int2): int2;
begin
  // #define MASKNONULL(t)	((t) & (SQLTYPE))
  Result := t and SQLTYPE;
end;

function ISSQLTYPE(t: int2): Boolean;
begin
  // #define ISSQLTYPE(t)	(MASKNONULL(t) >= SQLCHAR && MASKNONULL(t) < SQLMAXTYPES)
  t := MASKNONULL(t);
  Result := (t >= SQLCHAR) and (t < SQLMAXTYPES);
end;

function ISDEctype(t: int2): Boolean;
begin
  // #define ISDEctype(t) 	(MASKNONULL(t) == SQLDECIMAL || MASKNONULL(t) == SQLMONEY || MASKNONULL(t) == SQLDTIME || MASKNONULL(t) == SQLINTERVAL)
  Result := MASKNONULL(t) in [SQLDECIMAL, SQLMONEY, SQLDTIME, SQLINTERVAL];
end;

function ISNUMERIctype(t: int2): Boolean;
begin
  // #define ISNUMERIctype(t)	(MASKNONULL(t) == SQLSMINT || \
  //        MASKNONULL(t) == SQLINT || \
  //        MASKNONULL(t) == SQLINT8 || \
  //        MASKNONULL(t) == SQLFLOAT || \
  //        MASKNONULL(t) == SQLSMFLOAT || \
  //        MASKNONULL(t) == SQLMONEY || \
  //        MASKNONULL(t) == SQLSERIAL || \
  //        MASKNONULL(t) == SQLSERIAL8 || \
  //        MASKNONULL(t) == SQLDECIMAL)

  Result := MASKNONULL(t) in [SQLSMINT, SQLINT, SQLINT8, SQLFLOAT, SQLSMFLOAT,
    SQLMONEY, SQLSERIAL, SQLSERIAL8, SQLDECIMAL];
end;

function ISBLOBTYPE(sqltype: int2): Boolean;
begin
  // #define ISBLOBTYPE(type)	(ISBYTESTYPE (type) || ISTEXTTYPE(type))
  Result := ISBYTESTYPE(sqltype) or ISTEXTTYPE(sqltype);
end;

function ISBYTESTYPE(sqltype: int2): Boolean;
begin
  // #define ISBYTESTYPE(type)	(MASKNONULL(type) == SQLBYTES)
  Result := MASKNONULL(sqltype) = SQLBYTES;
end;

function ISTEXTTYPE(sqltype: int2): Boolean;
begin
  // #define ISTEXTTYPE(type)	(MASKNONULL(type) == SQLTEXT)
  Result := MASKNONULL(sqltype) = SQLTEXT;
end;

function ISSQLHOST(t: int2): Boolean;
begin
  // #define ISSQLHOST(t)      (((t) & SQLHOST) == SQLHOST)
  Result := (t and SQLHOST) = SQLHOST;
end;

function ISVCTYPE(t: int2): Boolean;
begin
  // #define ISVCTYPE(t)    (MASKNONULL(t) == SQLVCHAR || MASKNONULL(t) == SQLNVCHAR)
  Result := MASKNONULL(t) in [SQLVCHAR, SQLNVCHAR];
end;

function ISCHARTYPE(t: int2): Boolean;
begin
  // #define ISCHARTYPE(t) 	(MASKNONULL(t) == SQLCHAR || MASKNONULL(t) == SQLNCHAR)
  Result := MASKNONULL(t) in [SQLCHAR, SQLNCHAR];
end;

function ISNSTRINGTYPE(t: int2): Boolean;
begin
  // #define ISNSTRINGTYPE(t)	(MASKNONULL(t) == SQLNCHAR || MASKNONULL(t) == SQLNVCHAR)
  Result := MASKNONULL(t) in [SQLNCHAR, SQLNVCHAR];
end;

function ISSTRINGTYPE(t: int2): Boolean;
begin
  // #define ISSTRINGTYPE(t) 	(ISVCTYPE(t) || ISCHARTYPE(t))
  Result := ISVCTYPE(t) or ISCHARTYPE(t);
end;

function ISUDTVARTYPE(t: int2): Boolean;
begin
  // #define	ISUDTVARTYPE(t)		(MASKNONULL(t) == SQLUDTVAR)
  Result := MASKNONULL(t) = SQLUDTVAR;
end;

function ISUDTFIXEDTYPE(t: int2): Boolean;
begin
  // #define ISUDTFIXEDTYPE(t)	(MASKNONULL(t) == SQLUDTFIXED)
  Result := MASKNONULL(t) = SQLUDTFIXED;
end;

function ISUDTTYPE(t: int2): Boolean;
begin
  // #define	ISUDTTYPE(t)		(ISUDTVARTYPE(t) || ISUDTFIXEDTYPE(t))
  Result := (ISUDTVARTYPE(t) or ISUDTFIXEDTYPE(t));
end;

function ISCOMPLEXTYPE(t: int2): Boolean;
begin
  // #define ISCOMPLEXTYPE(t)	(ISROWTYPE(t) || ISCOLLTYPE(t))
  Result := (ISROWTYPE(t) or ISCOLLTYPE(t));
end;

function ISROWTYPE(t: int2): Boolean;
begin
  // #define ISROWTYPE(t)		(MASKNONULL(t) == SQLROW)
  Result := MASKNONULL(t) = SQLROW;
end;

function ISLISTTYPE(t: int2): Boolean;
begin
  // #define ISLISTTYPE(t)	   (MASKNONULL(t) == SQLLIST)
  Result := MASKNONULL(t) = SQLLIST;
end;

function ISMULTISETTYPE(t: int2): Boolean;
begin
  // #define ISMULTISETTYPE(t)	(MASKNONULL(t) == SQLMULTISET)
  Result := MASKNONULL(t) = SQLMULTISET;
end;

function ISREFTYPE(t: int2): Boolean;
begin
  // #define ISREFTYPE(t)		(MASKNONULL(t) == SQLROWREF)
  Result := MASKNONULL(t) = SQLROWREF;
end;

function ISSETTYPE(t: int2): Boolean;
begin
  // #define ISSETTYPE(t)		(MASKNONULL(t) == SQLSET)
  Result := MASKNONULL(t) = SQLSET;
end;

function ISCOLLECTTYPE(t: int2): Boolean;
begin
  // #define ISCOLLECTTYPE(t)	(MASKNONULL(t) == SQLCOLLECTION)
  Result := MASKNONULL(t) = SQLCOLLECTION;
end;

function ISCOLLTYPE(t: int2): Boolean;
begin
  //#define ISCOLLTYPE(t) 	(ISSETTYPE(t) || ISMULTISETTYPE(t) || ISLISTTYPE(t) || ISCOLLECTTYPE(t))
  Result := ISSETTYPE(t) or ISMULTISETTYPE(t) or ISLISTTYPE(t) or ISCOLLECTTYPE(t);
end;

function ISSERIALTYPE(t: int2): Boolean;
begin
  // #define ISSERIALTYPE(t)   	(((t) & SQLTYPE) == SQLSERIAL || ((t) & SQLTYPE) == SQLSERIAL8 || \
  //       ((t) & SQLTYPE) == SQLREFSER8)
  Result := MASKNONULL(t) in [SQLSERIAL, SQLSERIAL8, SQLREFSER8];
end;

function ISDISTINCTTYPE(t: int2): Boolean;
begin
  // #define ISDISTINCTTYPE(t)	((t) & SQLDISTINCT)
  Result := Boolean(t and SQLDISTINCT);
end;

function ISCSTTYPE(t: int2): Boolean;
begin
  // #define ISCSTTYPE(t)      (ISUDTTYPE(t) && ((t) & SQLCSTTYPE))
  Result := ISUDTTYPE(t) and Boolean(t and SQLCSTTYPE);
end;

function ISNONNLSCHAR(t: int2): Boolean;
begin
  // #define ISNONNLSCHAR(t)		(MASKNONULL(t) == SQLCHAR || \
  //        MASKNONULL(t) == SQLVCHAR)
  Result := MASKNONULL(t) in [SQLCHAR, SQLVCHAR];
end;

function ISOPTICALCOL(sqltype: Char): Boolean;
begin
  // #define ISOPTICALCOL(type)	(type == 'O')
  Result := sqltype = 'O';
end;

function ISLVARCHARXTYPE(t: int2; xid: int4): Boolean;
begin
  // #define ISLVARCHARXTYPE(t, xid)  (ISUDTTYPE((t)) && (xid) == XID_LVARCHAR)
  Result := ISUDTTYPE(t) and (xid = XID_LVARCHAR);
end;

function ISDISTINCTLVARCHAR(t: int2): Boolean;
begin
  // #define ISDISTINCTLVARCHAR(t)	((t) & SQLDLVARCHAR)
  Result := Boolean(t and SQLDLVARCHAR);
end;

function LIKELVARCHARXTYPE(t: int2; xid: int4): Boolean;
begin
  // #define LIKELVARCHARXTYPE(t,xid) ((ISLVARCHARXTYPE(t,xid)) || ISDISTINCTLVARCHAR(t))
  Result := ISLVARCHARXTYPE(t, xid) or ISDISTINCTLVARCHAR(t);
end;

function ISSMARTBLOB(sqltype: int2; xid: int4): Boolean;
begin
  // #define ISSMARTBLOB(type, xid)  (ISUDTTYPE((type)) && \
  //           ((xid == XID_CLOB) || (xid == XID_BLOB)))
  Result := ISUDTTYPE(sqltype) and ((xid = XID_CLOB) or (xid = XID_BLOB));
end;

function ISBOOLEANXTYPE(t: int2; xid: int4): Boolean;
begin
  // #define ISBOOLEANXTYPE(t, xid)  (ISUDTTYPE((t)) && (xid) == XID_BOOLEAN)
  Result := ISUDTTYPE(t) and (xid = XID_BOOLEAN);
end;

function ISDISTINCTBOOLEAN(t: int2): Boolean;
begin
  // #define ISDISTINCTBOOLEAN(t)	(((t) & SQLDBOOLEAN) && \
  //           (ISUDTTYPE(t)))
  Result := Boolean(t and SQLDBOOLEAN) and ISUDTTYPE(t);
end;

function LIKEBOOLEANXTYPE(t: int2; xid: int4): Boolean;
begin
  // #define LIKEBOOLEANXTYPE(t,xid) ((ISBOOLEANXTYPE(t,xid))||\
  //         ISDISTINCTBOOLEAN(t))
  Result := ISBOOLEANXTYPE(t, xid) or ISDISTINCTBOOLEAN(t);
end;

function ISFIXLENGTHTYPE(t: int2): Boolean;
begin
  // #define ISFIXLENGTHTYPE(t)	(!ISBYTESTYPE(t) && !ISTEXTTYPE(t) \
  //        && !ISCOMPLEXTYPE(t) \
  //        && !ISUDTVARTYPE(t) \
  //        && !ISVCTYPE(t))
  Result := not ISBYTESTYPE(t) and not ISTEXTTYPE(t) and not ISCOMPLEXTYPE(t)
    and not ISUDTVARTYPE(t) and not ISVCTYPE(t);
end;

procedure toctype(var ctype, sqltype: int2);
begin
(**
  extern int2 sqlctype[];

  #define toctype(ctype, type) \
      { \
      if (type == CINTTYPE) \
      { \
        if (sizeof(mint) == sizeof(mlong)) \
            { ctype = type = CLONGTYPE; } \
        else if (sizeof(mint) == sizeof(int2)) \
            { ctype = type = CSHORTTYPE; } \
        else \
            { ctype = CLONGTYPE; type = CINTTYPE; } \
      } \
      else if (type >= 0 && type < TYPEMAX) \
          ctype = sqlctype[type]; \
      else \
          ctype = type; \
      }
**)

  if sqltype <> CINTTYPE then
    if (sqltype >= 0) and (sqltype < TYPEMAX) then
      ctype := sqlctype[sqltype] else
      ctype := sqltype
  else
    if SizeOf(mint) = SizeOf(mlong) then
    begin
      ctype := CLONGTYPE;
      sqltype := CLONGTYPE;
    end
    else if SizeOf(mint) = SizeOf(int2) then
    begin
      ctype := CSHORTTYPE;
      sqltype := CSHORTTYPE;
    end
    else
    begin
      ctype := CLONGTYPE;
      sqltype := CINTTYPE;
    end;
end;


end.